<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="模板名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入模板名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['template:process:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['template:process:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['template:process:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['template:process:export']"
          >导出</el-button
        >
      </el-col>
    </el-row>

    <el-table
      v-loading="loading"
      :data="processList"
      @selection-change="handleSelectionChange"
      :span-method="handleColSpan"
      class="process-list"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="模板名称" prop="name" width="200">
        <template v-slot="{ row }">
          <div class="process-title">{{ row.name }}</div>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="allowSame" width="80">
        <template v-slot="{ row }">
          <el-tag :type="row.status ? 'success' : 'warning'">{{ row.status ? '正常' : '停用' }}</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="任务指派" align="center" prop="autoExec" width="80">
        <template v-slot="{ row }">
          <span>{{ row.autoExec ? '自动' : '手动' }}</span>
          <!-- <el-tag :type="row.allowSame ? 'success' : 'warning'">{{ row.allowSame ? '是' : '否' }}</el-tag> -->
        </template>
      </el-table-column>
      <el-table-column label="允许用户执行连续任务" align="center" prop="allowSame" width="80">
        <template v-slot="{ row }">
          <span>{{ row.allowSame ? '是' : '否' }}</span>
          <!-- <el-tag :type="row.allowSame ? 'success' : 'warning'">{{ row.allowSame ? '是' : '否' }}</el-tag> -->
        </template>
      </el-table-column>
      <el-table-column label="最大等待时间/分钟" align="center" prop="waitTime" width="80" />
      <el-table-column label="超时自动撤回" align="center" prop="autoCancel" width="80">
        <template v-slot="{ row }">
          <span>{{ row.autoCancel ? '是' : '否' }}</span>
          <!-- <el-tag :type="row.autoCancel ? 'success' : 'warning'">{{ row.autoCancel ? '是' : '否' }}</el-tag> -->
        </template>
      </el-table-column>
      <el-table-column label="模板描述" align="center" prop="description">
        <template v-slot="{ row }">
          <div class="process-desc">{{ row.description }}</div>
          <process-detail :items="row.items"></process-detail>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['template:process:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['template:process:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改流程模板对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="180px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="模板名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入模板名称" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="模板描述" prop="description">
              <el-input v-model="form.description" type="textarea" placeholder="请输入模板描述" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="最大等待时间" prop="waitTime">
              <el-input-number
                controls-position="right"
                v-model="form.waitTime"
                placeholder="最大等待时间/分钟,-1不设置"
                :min="-1"
                :step="30"
                style="width: 100%"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否超时自动撤回">
              <el-radio-group v-model="form.autoCancel">
                <el-radio :label="1">是</el-radio>
                <el-radio :label="0">否</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态">
              <el-radio-group v-model="form.status">
                <el-radio v-for="dict in statusOptions" :key="dict.dictValue" :label="Number(dict.dictValue)">{{
                  dict.dictLabel
                }}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="允许用户执行连续任务">
              <el-radio-group v-model="form.allowSame">
                <el-radio :label="1">是</el-radio>
                <el-radio :label="0">否</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <process-editor v-model="form.items"></process-editor>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import ProcessEditor from './ProcessEditor'
import ProcessDetail from './ProcessDetail'

import { listProcess, getProcess, delProcess, addProcess, updateProcess, exportProcess } from '@/api/template/process'

export default {
  name: 'Process',
  components: { ProcessEditor, ProcessDetail },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 流程模板表格数据
      processList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        status: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [{ required: true, message: '模板名称不能为空', trigger: 'blur' }],
        status: [{ required: true, message: '状态不能为空', trigger: 'blur' }],
        allowSame: [{ required: true, message: '是否允许同一个用户执行连续任务不能为空', trigger: 'blur' }],
        autoCancel: [{ required: true, message: '是否超时自动撤回不能为空', trigger: 'blur' }],
      },
      statusOptions: [],
    }
  },
  created() {
    this.getList()
    this.getDicts('sys_normal_status').then((response) => {
      this.statusOptions = response.data
    })
  },
  methods: {
    /** 查询流程模板列表 */
    getList() {
      this.loading = true
      listProcess(this.queryParams).then((response) => {
        this.processList = response.rows
        this.total = response.total
        this.loading = false
      })
    },

    slotv(abc) {
      console.log(abc)
      return abc
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        deptId: undefined,
        name: undefined,
        description: undefined,
        status: 1,
        allowSame: 1,
        waitTime: -1,
        autoCancel: 1,
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加流程模板'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getProcess(id).then((response) => {
        this.form = response.data
        this.open = true
        this.title = '修改流程模板'
      })
    },
    /** 提交按钮 */
    submitForm() {
      const { form } = this
      if (!form.items || form.items.length <= 4) {
        return this.msgError('请检查流程是否正确!')
      }

      const ajaxFn = form.id ? updateProcess : addProcess
      const msg = form.id ? '修改成功' : '添加成功'
      this.$refs['form'].validate((valid) => {
        if (valid) {
          ajaxFn(form).then((response) => {
            if (response.code === 200) {
              this.msgSuccess(msg)
              this.open = false
              this.getList()
            } else {
              this.msgError(response.msg)
            }
          })
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$confirm('是否确认删除流程模板编号为"' + ids + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(function () {
          return delProcess(ids)
        })
        .then(() => {
          this.getList()
          this.msgSuccess('删除成功')
        })
        .catch(function () {})
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$confirm('是否确认导出所有流程模板数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(function () {
          return exportProcess(queryParams)
        })
        .then((response) => {
          this.download(response.msg)
        })
        .catch(function () {})
    },

    // 表格列合并
    handleColSpan({ row, column, rowIndex, columnIndex }) {
      return {
        rowspan: 1,
        colspan: 1,
      }
      if (columnIndex == 1) {
        return {
          rowspan: 0,
          colspan: 3,
        }
      } else {
        return {
          rowspan: 1,
          colspan: 1,
        }
      }
    },
  },
}
</script>

<style lang="scss" scoped>
.process-list {
  .process-title {
    font-size: 20px;
    font-weight: 500;
    text-align: left;
  }
  .process-status {
  }

  .process-desc {
    text-align: left;
    padding: 6px 18px;
    font-size: 16px;
    text-overflow: ellipsis;
  }
}
</style>
